package com.yswy.app.config;


import com.google.common.base.Charsets;
import com.google.common.hash.Funnel;
import com.yswy.common.utils.bloom.BloomFilterHelper;
import com.yswy.common.utils.bloom.BloomRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


/**
 * @author Rocky
 * @version 1.0
 * @description
 * @email inaho00@foxmail.com
 * @createDate 2021/6/10 下午3:23
 * @log
 */
@Slf4j
@Configuration
public class BloomFilterConfig implements InitializingBean{

	@Autowired
	RedisTemplate<Object, Object> redisTemplate;

	@Bean
	public BloomFilterHelper<String> initBloomFilterHelper() {
		return new BloomFilterHelper<>((Funnel<String>) (from, into) -> into.putString(from, Charsets.UTF_8)
				.putString(from, Charsets.UTF_8), 1000000, 0.01);
	}

	/**
	 * 布隆过滤器bean注入
	 * @return
	 */
	@Bean
	public BloomRedisService bloomRedisService(){
		BloomRedisService bloomRedisService = new BloomRedisService();
		bloomRedisService.setBloomFilterHelper(initBloomFilterHelper());
		bloomRedisService.setRedisTemplate(redisTemplate);
		return bloomRedisService;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
	}
}
